package com.example.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginPage extends AnyPage {

    private WebElement username;
    private WebElement password;
    private WebElement submit;
    private WebElement loginform;

    @FindBy(xpath = "//label[@class='error'][@for = 'username']")
    private WebElement errorUserName;

    @FindBy(xpath = "//label[@class='error'][@for = 'password']")
    private WebElement errorPassword;

    @FindBy(xpath = ".//*[@colspan=2]")
    private WebElement incorrectLorP;

    public boolean errorUserNameIsDisplayed() {
        return this.errorUserName.isDisplayed();
    }

    public boolean errorPasswordIsDisplayed() {
        return this.errorPassword.isDisplayed();
    }

    public boolean errorIncorrect() {
        return this.incorrectLorP.isDisplayed();
    }

    public boolean isOnThisPage() {
        return loginform.isDisplayed();
    }

    public MovieListPage loginWithValidCredentials(String username, String password) {
        this.username.sendKeys(username);
        this.password.sendKeys(password);
        submit.click();
        return MyPageFactory.getPage(driver, MovieListPage.class);
    }

    public LoginPage loginWithWrongCredentials(String username, String password) {
        this.username.sendKeys(username);
        this.password.sendKeys(password);
        submit.click();
        return MyPageFactory.getPage(driver, LoginPage.class);
    }

    @Override
    void tryToOpen() {
        MyPageFactory.getPage(driver, InternalPage.class).logout();
    }
}
